<template>
  <div class="dashboard">
    <div class="dashboard-header">
      <h2>系统概览</h2>
      <div class="date-info">
        <span>{{ currentDate }}</span>
        <span>{{ currentTime }}</span>
      </div>
    </div>
    
    <!-- 统计卡片 -->
    <div class="stats-cards">
      <el-card class="stat-card">
        <div class="stat-content">
          <div class="stat-info">
            <p class="stat-label">学生总数</p>
            <h3 class="stat-value">{{ studentCount }}</h3>
          </div>
          <div class="stat-icon">
            <el-icon><user /></el-icon>
          </div>
        </div>
      </el-card>
      
      <el-card class="stat-card">
        <div class="stat-content">
          <div class="stat-info">
            <p class="stat-label">班级总数</p>
            <h3 class="stat-value">{{ classCount }}</h3>
          </div>
          <div class="stat-icon">
            <el-icon><school /></el-icon>
          </div>
        </div>
      </el-card>
      
      <el-card class="stat-card">
        <div class="stat-content">
          <div class="stat-info">
            <p class="stat-label">课程总数</p>
            <h3 class="stat-value">{{ courseCount }}</h3>
          </div>
          <div class="stat-icon">
            <el-icon><document /></el-icon>
          </div>
        </div>
      </el-card>
      
      <el-card class="stat-card">
        <div class="stat-content">
          <div class="stat-info">
            <p class="stat-label">今日考勤</p>
            <h3 class="stat-value">{{ attendanceRate }}%</h3>
          </div>
          <div class="stat-icon">
            <el-icon><check /></el-icon>
          </div>
        </div>
      </el-card>
    </div>
    
    <!-- 图表区域 -->
    <div class="charts-section">
      <el-card class="chart-card">
        <template #header>
          <div class="card-header">
            <span>学生分布统计</span>
          </div>
        </template>
        <div class="chart-content">
          <!-- 这里将使用图表组件，目前显示模拟数据 -->
          <div class="mock-chart">
            <p>学生分布图（按年级）</p>
            <div class="chart-bars">
              <div class="chart-bar" :style="{height: '60%'}"><span>一年级</span></div>
              <div class="chart-bar" :style="{height: '75%'}"><span>二年级</span></div>
              <div class="chart-bar" :style="{height: '90%'}"><span>三年级</span></div>
              <div class="chart-bar" :style="{height: '70%'}"><span>四年级</span></div>
              <div class="chart-bar" :style="{height: '50%'}"><span>五年级</span></div>
            </div>
          </div>
        </div>
      </el-card>
      
      <el-card class="chart-card">
        <template #header>
          <div class="card-header">
            <span>最近活动</span>
          </div>
        </template>
        <div class="recent-activities">
          <el-timeline>
            <el-timeline-item timestamp="09:41">
              <p>新增学生：张三</p>
            </el-timeline-item>
            <el-timeline-item timestamp="昨天 14:25">
              <p>创建新课程：高等数学</p>
            </el-timeline-item>
            <el-timeline-item timestamp="2023-09-24 10:30">
              <p>更新班级信息：计算机科学一班</p>
            </el-timeline-item>
            <el-timeline-item timestamp="2023-09-23 16:15">
              <p>完成月考成绩录入</p>
            </el-timeline-item>
          </el-timeline>
        </div>
      </el-card>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted, onUnmounted } from 'vue'
import { User, School, Document, Check } from '@element-plus/icons-vue'
import axios from 'axios'
import { ElMessage } from 'element-plus'

// 统计数据
const studentCount = ref('1,254')
const classCount = ref('36')
const courseCount = ref('142')
const attendanceRate = ref('96.8')

// 当前日期和时间
const currentDate = ref('')
const currentTime = ref('')

// 更新日期和时间的函数
const updateDateTime = () => {
  const now = new Date()
  
  // 格式化日期
  const dateOptions = {
    year: 'numeric',
    month: 'long',
    day: 'numeric',
    weekday: 'long'
  }
  currentDate.value = now.toLocaleDateString('zh-CN', dateOptions)
  
  // 格式化时间
  const timeOptions = {
    hour: '2-digit',
    minute: '2-digit',
    second: '2-digit'
  }
  currentTime.value = now.toLocaleTimeString('zh-CN', timeOptions)
}

// 定时器
let timer = null

onMounted(() => {
  // 初始化日期和时间
  updateDateTime()
  
  // 设置定时器，每秒更新一次时间
  timer = setInterval(updateDateTime, 1000)
  
  // 初始加载数据
  loadDashboardData()
})

onUnmounted(() => {
  // 清除定时器
  if (timer) {
    clearInterval(timer)
  }
})

// 加载仪表盘数据
const loadDashboardData = async () => {
  const dashboardLoading = ref(true)
  try {
    // 真实的API调用
    const response = await axios.get('/api/dashboard/stats')
    studentCount.value = response.data.studentCount
    classCount.value = response.data.classCount
    courseCount.value = response.data.courseCount
    attendanceRate.value = response.data.attendanceRate
  } catch (error) {
    console.error('加载仪表盘数据失败:', error)
    ElMessage.error('加载仪表盘数据失败，请稍后重试')
  } finally {
    dashboardLoading.value = false
  }
}
</script>

<style scoped>
.dashboard {
  height: 100%;
}

.dashboard-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 24px;
}

.dashboard-header h2 {
  margin: 0;
  font-size: 20px;
  color: #333;
}

.date-info {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  color: #666;
}

.stats-cards {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 16px;
  margin-bottom: 24px;
}

.stat-card {
  transition: all 0.3s ease;
  cursor: pointer;
}

.stat-card:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}

.stat-content {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.stat-info {
  flex: 1;
}

.stat-label {
  margin: 0 0 4px 0;
  font-size: 14px;
  color: #666;
}

.stat-value {
  margin: 0;
  font-size: 24px;
  font-weight: bold;
  color: #1890ff;
}

.stat-icon {
  width: 48px;
  height: 48px;
  background-color: #f0f9ff;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #1890ff;
  font-size: 20px;
}

.charts-section {
  display: grid;
  grid-template-columns: 2fr 1fr;
  gap: 16px;
}

.chart-card {
  height: 300px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.chart-content {
  height: calc(100% - 48px);
  display: flex;
  align-items: center;
  justify-content: center;
}

.mock-chart {
  text-align: center;
  width: 100%;
}

.chart-bars {
  display: flex;
  align-items: flex-end;
  justify-content: space-around;
  height: 180px;
  margin-top: 20px;
}

.chart-bar {
  width: 40px;
  background: linear-gradient(to top, #1890ff, #69c0ff);
  border-radius: 4px 4px 0 0;
  position: relative;
  transition: height 1s ease;
}

.chart-bar span {
  position: absolute;
  bottom: -25px;
  left: 50%;
  transform: translateX(-50%);
  font-size: 12px;
  color: #666;
  white-space: nowrap;
}

.recent-activities {
  height: calc(100% - 48px);
  overflow-y: auto;
}

.recent-activities .el-timeline {
  padding: 0;
}

.recent-activities .el-timeline-item__wrapper {
  padding-bottom: 16px;
}

@media (max-width: 768px) {
  .charts-section {
    grid-template-columns: 1fr;
  }
  
  .stats-cards {
    grid-template-columns: repeat(2, 1fr);
  }
}

@media (max-width: 480px) {
  .stats-cards {
    grid-template-columns: 1fr;
  }
  
  .dashboard-header {
    flex-direction: column;
    align-items: flex-start;
    gap: 8px;
  }
  
  .date-info {
    align-items: flex-start;
  }
}
</style>